﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_points_for : System.Web.UI.Page
{
    // Fields

    protected PointsFor pf = new PointsFor();
    protected PointsForManager pfm = new PointsForManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.pf.Name = this.txtName.Value;
            this.pf.IfGift = this.rdIfGift1.Checked ? 1 : 0;
            this.pf.ExchangePoints = Convert.ToInt32(this.txtExchangePoints.Value);
            this.pf.Descript = this.fckDescript.Value;
            this.pf.LanguageVersion = this.Session["language_version"].ToString();
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                DataTable pointsForById = this.pfm.GetPointsForById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (this.filePointsFor.PostedFile.FileName != "")
                {
                    this.pf.Image = "/cn/points_for/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + Path.GetExtension(this.filePointsFor.PostedFile.FileName);
                    File.Delete(base.Server.MapPath(pointsForById.Rows[0]["image"].ToString()));
                }
                else
                {
                    this.pf.Image = pointsForById.Rows[0]["image"].ToString();
                }
                this.pf.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.pfm.ModifyPointsFor(this.pf))
                {
                    if (this.filePointsFor.PostedFile.FileName != "")
                    {
                        this.filePointsFor.PostedFile.SaveAs(base.Server.MapPath(this.pf.Image));
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"points_for_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else
            {
                this.pf.Image = "/cn/points_for/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + Path.GetExtension(this.filePointsFor.PostedFile.FileName);
                if (this.pfm.AddPointsFor(this.pf))
                {
                    if (this.filePointsFor.PostedFile.FileName != "")
                    {
                        this.filePointsFor.PostedFile.SaveAs(base.Server.MapPath(this.pf.Image));
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"points_for_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑商品");
                return builder;
            }
            builder.Append("添加商品");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.PointsForBind();
            }
            else
            {
                this.imgPointsFor.Style.Add("display", "none");
                this.rdIfGift0.Checked = true;
            }
        }
    }

    private void PointsForBind()
    {
        try
        {
            DataTable pointsForById = this.pfm.GetPointsForById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (pointsForById.Rows.Count > 0)
            {
                DataRow row = pointsForById.Rows[0];
                this.txtName.Value = row["name"].ToString();
                if (Convert.ToInt32(row["ifGift"]) == 1)
                {
                    this.rdIfGift1.Checked = true;
                }
                else
                {
                    this.rdIfGift0.Checked = true;
                }
                this.imgPointsFor.Src = row["image"].ToString();
                this.txtExchangePoints.Value = row["exchangePoints"].ToString();
                this.fckDescript.Value = row["descript"].ToString();
            }
        }
        catch
        {
        }
    }

}